package com.iisigroup.http.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.ThreadLocalRandom;

import org.apache.commons.io.IOUtils;

public class RandomAccessFileDemo {
	public static void main(String[] args) throws IOException {
//		BufferedReader br = new BufferedReader(new FileReader(new File("/home/weblogic/Desktop/person2.csv")));
//		
//		String line = br.readLine();
//		while(line!=null){
//			System.out.println(line);
//			line = br.readLine();
//		}
		new RandomAccessFileDemo().getRandomPersonId();
	}
	private String getRandomPersonId(){
		String result =null;
		try {
			// create a new RandomAccessFile with filename test
			RandomAccessFile raf = new RandomAccessFile(new File(
					"/home/weblogic/Desktop/person2.csv"), "r");
			int lines =(int) raf.length()/11;		
			int randomInt = ThreadLocalRandom.current().nextInt(lines);			
			raf.seek(randomInt * 11);
			String pId = raf.readLine();
			if (pId.length() == 10) {
				System.out.println(pId);
				result = pId;
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		return result ;
	}
	public static void main001(){
		try {
			// create a new RandomAccessFile with filename test
			RandomAccessFile raf = new RandomAccessFile(new File(
					"/home/weblogic/Desktop/person2.csv"), "r");
			int lines =(int) raf.length()/11;
			System.out.println("lines: "+lines);
			for (int i = 1; i < raf.length(); ++i) {
				raf.seek(i * 11);
				String pId = raf.readLine();
				if (pId.length() == 10) {
					System.out.println(pId);
				}
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
}
